package com.yanqu.road.server.manager.unioncampwar;

import com.yanqu.road.dao.impl.unioncampwar.*;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.entity.unioncampwar.*;
import com.yanqu.road.entity.unioncampwar.cross.CrossUnionCampWarGroupUnionData;
import com.yanqu.road.logic.config.unioncampwar.UnionCampWarConfig;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionCampWarPb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.unionCampWar.UnionCampWar;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarActivity {
    private int weekId;
    private int centerGroupId;
    private int groupId;
    private UnionCampWarCampData campData;
    /**
     * 门客数据是否全部同步完
     */
    private boolean isAllSync = false;
    /**
     * key:userId
     */
    private Map<Long, UnionCampWarUserData> userDataMap = new ConcurrentHashMap<>();
    /**
     * key:unionId
     */
    private Map<String, UnionCampWarHoldData> holdDataMap = new ConcurrentHashMap<>();
    /**
     * key:campId
     */
    private Map<Integer, UnionCampWarCampInfo> campInfoMap = new ConcurrentHashMap<>();
    /**
     * 待入库公告数据
     */
    private List<UnionCampWarNoticeData> needInserrNoticeList = new ArrayList<>();
    /**
     * 公告数据
     */
    private List<UnionCampWar.UnionCampWarMarqueeInfoTemp> marqueeMsgList = new ArrayList<>();
    /**
     * 待入库宝库公告
     */
    private List<UnionCampWarBoxNoticeData> needInsertBoxNoticeList = new ArrayList<>();

    /**
     * 副本聊天
     */
    private WorldMessageProto.WorldMessageListMsg.Builder chatListMsg = WorldMessageProto.WorldMessageListMsg.newBuilder();

    private RandomHelper random = new RandomHelper();

    public UnionCampWarActivity(int centerGroupId, int groupId, int weekId) {
        this.centerGroupId = centerGroupId;
        this.groupId = groupId;
        this.weekId = weekId;
    }

    public void save() {
        if (campData.isInsertOption()) {
            new UnionCampWarCampDaoImpl().add(campData);
        } else if (campData.isUpdateOption()) {
            new UnionCampWarCampDaoImpl().update(campData);
        }

        List<UnionCampWarUserData> userDataList = new ArrayList<>(userDataMap.values());
        for (UnionCampWarUserData userData : userDataList) {
            if (userData.isInsertOption()) {
                new UnionCampWarUserDaoImpl().add(userData);
            } else if (userData.isUpdateOption()) {
                new UnionCampWarUserDaoImpl().update(userData);
            }
        }

        List<UnionCampWarHoldData> holdDataList = new ArrayList<>(holdDataMap.values());
        for (UnionCampWarHoldData holdData : holdDataList) {
            if (holdData.isInsertOption()) {
                new UnionCampWarHoldDaoImpl().add(holdData);
            } else if (holdData.isUpdateOption()) {
                new UnionCampWarHoldDaoImpl().update(holdData);
            }
        }

        for (UnionCampWarNoticeData noticeData : popNeedInsertNoticeDataList()) {
            new UnionCampWarNoticeDaoImpl().add(noticeData);
        }

        for (UnionCampWarBoxNoticeData boxNoticeData : popNeedInsertBoxNoticeDataList()) {
            new UnionCampWarBoxNoticeDaoImpl().add(boxNoticeData);
        }


    }

    public UnionCampWarCampData getCampData() {
        return campData;
    }

    public void setCampData(UnionCampWarCampData campData) {
        this.campData = campData;
    }

    public void addCampInfo(UnionCampWarCampInfo campInfo) {
        campInfoMap.put(campInfo.getCampId(), campInfo);
    }

    public void addHoldData(UnionCampWarHoldData holdData) {
        UnionCampWarCampInfo campInfo = campInfoMap.get(holdData.getCampId());
        if (campInfo == null) {
            campInfo = new UnionCampWarCampInfo(holdData.getCampId());
            campInfoMap.put(holdData.getCampId(), campInfo);
        }
        campInfo.addHoldInfo(holdData);
        holdDataMap.put(holdData.getUnionId(), holdData);
    }

    public void initByDb(UnionCampWarCampData campData, List<UnionCampWarHoldData> holdDataList, List<UnionCampWarUserData> userDataList, List<UnionCampWarNoticeData> noticeDataList, List<UnionCampWarBoxNoticeData> boxNoticeDataList) {
        this.campData = campData;
        campInfoMap.clear();
        for (UnionCampWarHoldData holdData : holdDataList) {
            addHoldData(holdData);
        }
        for (UnionCampWarUserData userData : userDataList) {
            int campId = userData.getCampId();
            UnionCampWarCampInfo campInfo = campInfoMap.get(campId);
            if (campInfo == null) {
                campInfo = new UnionCampWarCampInfo(campId);
                campInfoMap.put(campId, campInfo);
            }
            campInfo.addUserData(userData);

            userDataMap.put(userData.getUserId(), userData);

            campInfo.updateLastWinTime(userData.getLastAttackTime());
        }

        for (UnionCampWarCampInfo campInfo : campInfoMap.values()) {
            campInfo.initCampHp();
        }

        for (UnionCampWarNoticeData noticeData : noticeDataList) {
            PlayerProto.PlayerBaseTempMsg.Builder playerBaseTempMsg = PlayerProto.PlayerBaseTempMsg.newBuilder();
            CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(noticeData.getAttackUserId());
            if (unionMember != null) {
                playerBaseTempMsg = PlayerBasePb.parsePlayerBaseTempMsg(unionMember.getUserBaseInfo());
            }
            UnionCampWar.UnionCampWarMarqueeInfoTemp.Builder noticeMsg = UnionCampWarPb.parseMarqueeInfoMsg(noticeData, playerBaseTempMsg);
            marqueeMsgList.add(noticeMsg.build());
            while (marqueeMsgList.size() >= ConfigMgr.getUnionCampWarConfig().getNoticeNumMax()) {
                marqueeMsgList.remove(0);
            }
        }

        for (UnionCampWarBoxNoticeData boxNoticeData : boxNoticeDataList) {
            boxNoticeData.getCampId();
            UnionCampWarCampInfo campInfo = getCampInfo(boxNoticeData.getCampId());
            campInfo.addBoxNoticeMsg(UnionCampWarPb.parseBoxNoticeTempMsg(boxNoticeData).build());
        }


    }

    public UnionCampWarUserData getWarUserData(long userId) {
        return userDataMap.get(userId);
    }

    public UnionCampWarUserData getAndNewWarUserData(long userId, int campId, long serverId) {
        UnionCampWarUserData warUserData = userDataMap.get(userId);
        if (null == warUserData) {
            synchronized (userDataMap) {
                warUserData = userDataMap.get(userId);
                if (null == warUserData) {
                    warUserData = new UnionCampWarUserData(centerGroupId, groupId, campId, userId, serverId, weekId);
                    userDataMap.put(userId, warUserData);

                    UnionCampWarCampInfo campInfo = getCampInfo(warUserData.getCampId());
                    campInfo.addUserData(warUserData);
                }
            }
        }

        return warUserData;
    }

    public int getWeekId() {
        return weekId;
    }

    public int getCenterGroupId() {
        return centerGroupId;
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public UnionCampWarCampInfo getCampInfo(int campId) {
        return campInfoMap.get(campId);
    }

    /**
     * 主界面数据
     */
    public UnionCampWar.UnionCampWarMainScenceRespMsg.Builder getMainScenceMsg(long userId) {
        UnionCampWar.UnionCampWarMainScenceRespMsg.Builder msg = UnionCampWar.UnionCampWarMainScenceRespMsg.newBuilder();
        msg.setRet(0);
        UnionCampWarCampInfo leftCampInfo = getCampInfo(1);
        UnionCampWarCampInfo rightCampInfo = getCampInfo(2);
        UnionCampWar.UnionCampWarCampInfoTemp.Builder leftCampTemp = leftCampInfo.parseCampMsg();
        leftCampTemp.addAllRouteInfo(getRouteTemp());
        msg.setLeftCampInfo(leftCampTemp);
        UnionCampWar.UnionCampWarCampInfoTemp.Builder rightCampTemp = rightCampInfo.parseCampMsg();
        rightCampTemp.addAllRouteInfo(getRouteTemp());
        msg.setRightCampInfo(rightCampTemp);
        synchronized (marqueeMsgList) {
            msg.addAllMarqueeInfo(marqueeMsgList);
        }
        msg.setOwnCampId(0);
        msg.setMyRank(-1);

        UnionCampWarUserData warUserData = getWarUserData(userId);
        if (warUserData != null) {
            msg.setMyScore(warUserData.getScore());
//            UnionCampWarCampInfo campInfo = getCampInfo(warUserData.getCampId());
//            if (campInfo != null) {
//                msg.setMyRank(campInfo.getUserRank(userId));
//            }
            if (warUserData.getGetReward() == 1) {
                msg.setGetRankReward(true);
            }
        }
        if (campData.getWinCampId() != 0) {
            msg.setWinCampId(campData.getWinCampId());
        }

        for (BigDecimal ability : campData.getAvgAbilityList()) {
            msg.addAvgAbility(ability.toString());
        }

        return msg;
    }

    public List<UnionCampWar.UnionCampWarHoldRouteTemp> getRouteTemp() {
        List<UnionCampWar.UnionCampWarHoldRouteTemp> routeTempList = new ArrayList<>();
        for (List<Integer> list : campData.getRouteListMap().values()) {
            UnionCampWar.UnionCampWarHoldRouteTemp.Builder builder = UnionCampWar.UnionCampWarHoldRouteTemp.newBuilder();
            builder.addAllHoldId(list);
            routeTempList.add(builder.build());
        }
        return routeTempList;
    }

    public UnionCampWar.UnionCampWarMarqueeInfoTemp.Builder addNoticeData(UnionCampWarNoticeData noticeData) {
        synchronized (needInserrNoticeList) {
            needInserrNoticeList.add(noticeData);
        }

        PlayerProto.PlayerBaseTempMsg.Builder playerBaseTempMsg = PlayerProto.PlayerBaseTempMsg.newBuilder();
        CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(noticeData.getAttackUserId());
        if (unionMember != null) {
            playerBaseTempMsg = PlayerBasePb.parsePlayerBaseTempMsg(unionMember.getUserBaseInfo());
        }

        UnionCampWar.UnionCampWarMarqueeInfoTemp.Builder noticeMsg = UnionCampWarPb.parseMarqueeInfoMsg(noticeData, playerBaseTempMsg);
        synchronized (marqueeMsgList) {
            marqueeMsgList.add(noticeMsg.build());
            while (marqueeMsgList.size() >= ConfigMgr.getUnionCampWarConfig().getNoticeNumMax()) {
                marqueeMsgList.remove(0);
            }
        }

        return noticeMsg;
    }

    public List<UnionCampWarNoticeData> popNeedInsertNoticeDataList() {
        synchronized (needInserrNoticeList) {
            List<UnionCampWarNoticeData> list = new ArrayList<>(needInserrNoticeList);
            needInserrNoticeList.clear();
            return list;
        }
    }

    public void initCampBox() {
        UnionCampWarConfig config = ConfigMgr.getUnionCampWarConfig();
        if (campData.getLeftBoxRound() == 0) {
            campData.setLeftBoxRound(1);
            int bigBoxId = random.next(config.getBoxNum()) + 1;
            campData.setLeftBigBoxId(bigBoxId);
            Property randomReward = RandomWeightConfigHelper.getPropertyWeightItem(config.getSpecialLotteryList(getCenterGroupId()));
            campData.setLeftBigReward(PropertyHelper.parsePropertyToString(randomReward));
        }

        if (campData.getRightBoxRound() == 0) {
            campData.setRightBoxRound(1);
            int bigBoxId = random.next(config.getBoxNum()) + 1;
            campData.setRightBigBoxId(bigBoxId);
            Property randomReward = RandomWeightConfigHelper.getPropertyWeightItem(config.getSpecialLotteryList(getCenterGroupId()));
            campData.setRightBigReward(PropertyHelper.parsePropertyToString(randomReward));
        }
    }

    public void nextBoxRound(int campId) {
        UnionCampWarConfig config = ConfigMgr.getUnionCampWarConfig();
        if (1 == campId) {
            campData.setLeftBoxRound(campData.getLeftBoxRound() + 1);
            int bigBoxId = random.next(config.getBoxNum()) + 1;
            campData.setLeftBigBoxId(bigBoxId);
            Property randomReward = RandomWeightConfigHelper.getPropertyWeightItem(config.getSpecialLotteryList(getCenterGroupId()));
            campData.setLeftBigReward(PropertyHelper.parsePropertyToString(randomReward));
            campData.getLeftOpenBoxInfoMap().clear();
        } else {
            campData.setRightBoxRound(campData.getRightBoxRound() + 1);
            int bigBoxId = random.next(config.getBoxNum()) + 1;
            campData.setRightBigBoxId(bigBoxId);
            Property randomReward = RandomWeightConfigHelper.getPropertyWeightItem(config.getSpecialLotteryList(getCenterGroupId()));
            campData.setRightBigReward(PropertyHelper.parsePropertyToString(randomReward));
            campData.getRightOpenBoxInfoMap().clear();
        }
        campData.setUpdateOption();
    }

    public String getCampBoxBigRewards(int campId) {
        UnionCampWarConfig config = ConfigMgr.getUnionCampWarConfig();
        Property randomReward = RandomWeightConfigHelper.getPropertyWeightItem(config.getSpecialLotteryList(getCenterGroupId()));
        return PropertyHelper.parsePropertyToString(randomReward);
        //改成开到才随机
//        if (1 == campId) {
//            return campData.getLeftBigReward();
//        } else {
//            return campData.getRightBigReward();
//        }
    }

    /**
     * 商帮结算排名
     */
    public void warEnd() {
        UnionCampWarCampInfo leftCampInfo = getCampInfo(1);
        UnionCampWarCampInfo rightCampInfo = getCampInfo(2);
        List<UnionCampWarCampInfo> campInfoList = new ArrayList<>();
        campInfoList.add(leftCampInfo);
        campInfoList.add(rightCampInfo);
        if (this.campData.getWinCampId() == 0) {
            campInfoList.sort((o1, o2) -> {
                int compare = Integer.compare(o2.getFirstHoldLeftHp(), o1.getFirstHoldLeftHp());
                if (0 == compare) {
                    compare = Long.compare(o1.getFirstHoldLastBattleTime(), o2.getFirstHoldLastBattleTime());
                }
                if (0 == compare) {
                    compare = Integer.compare(o2.getLeftHp(), o1.getLeftHp());
                }
                if (0 == compare) {
                    compare = Long.compare(o1.getLastWinTime(), o2.getLastWinTime());
                }
                return compare;
            });
            //设置胜利阵营
            this.campData.setWinCampId(campInfoList.get(0).getCampId());
        }
        this.campData.setEndTime(DateHelper.getCurrentTime());

        this.campData.setLeftCampHp(leftCampInfo.getLeftHp());
        this.campData.setRightCampHp(rightCampInfo.getLeftHp());

        //更新玩家排名
        leftCampInfo.updateUserRank();
        rightCampInfo.updateUserRank();
    }

    /**
     * 结束检查,先打掉对面第一的据点血就算胜利
     *
     * @return
     */
    public void winnerCheck() {
        UnionCampWarCampInfo leftCampInfo = getCampInfo(1);
        UnionCampWarCampInfo rightCampInfo = getCampInfo(2);
        int firstHoldLeftHp1 = leftCampInfo.getFirstHoldLeftHp();
        int firstHoldLeftHp2 = rightCampInfo.getFirstHoldLeftHp();
        if (firstHoldLeftHp1 > 0 && firstHoldLeftHp2 > 0) {
            return;
        }
        int winnerCampId = 0;
        long firstHoldLastBattleTime1 = leftCampInfo.getFirstHoldLastBattleTime();
        long firstHoldLastBattleTime2 = rightCampInfo.getFirstHoldLastBattleTime();
        if (firstHoldLeftHp1 == 0 && firstHoldLeftHp2 == 0) {
            if (firstHoldLastBattleTime1 <= firstHoldLastBattleTime2) {
                winnerCampId = rightCampInfo.getCampId();
            } else {
                winnerCampId = leftCampInfo.getCampId();
            }
        } else if (firstHoldLeftHp1 == 0) {
            winnerCampId = rightCampInfo.getCampId();
        } else {
            winnerCampId = leftCampInfo.getCampId();
        }
        //设置胜利阵营
        this.campData.setWinCampId(winnerCampId);
    }

    public List<UnionCampWarHoldData> getNeedSyncPatronsHold() {
        List<UnionCampWarHoldData> list = new ArrayList<>();
        for (UnionCampWarCampInfo campInfo : campInfoMap.values()) {
            for (UnionCampWarHoldData holdData : campInfo.getHoldInfoMap().values()) {
                if (holdData.getPatronsNum() == 0) {
                    list.add(holdData);
                }
            }
        }

        return list;
    }

    public boolean isAllSync() {
        return isAllSync;
    }

    public void setAllSync(boolean allSync) {
        isAllSync = allSync;
    }

    public UnionCampWarHoldData getHoldData(String unionId) {
        UnionCampWarHoldData holdData = holdDataMap.get(unionId);
        return holdData;
    }

    public List<UnionCampWarHoldData> getHoldDataList(int holdId) {
        List<UnionCampWarHoldData> list = new ArrayList<>();
        for (UnionCampWarHoldData holdData : holdDataMap.values()) {
            if (holdData.getHoldId() == holdId) {
                list.add(holdData);
            }
        }
        return list;
    }

    public void updateHoldPatronsNum(String unionId, int patronsNum) {
        UnionCampWarHoldData holdData = getHoldData(unionId);
        if (null != holdData) {
            holdData.setPatronsNum(patronsNum);
        }

    }

    public void parseCampBoxScence(int campId, UnionCampWar.UnionCampBoxScenceRespMsg.Builder boxScenceMsg) {
        Map<Integer, UnionCampWarOpenBoxInfo> openBoxInfoMap;
        if (campId == 1) {
            boxScenceMsg.setRound(campData.getLeftBoxRound());
            boxScenceMsg.setBigReward(campData.getLeftBigReward());
            openBoxInfoMap = campData.getLeftOpenBoxInfoMap();
        } else {
            boxScenceMsg.setRound(campData.getRightBoxRound());
            boxScenceMsg.setBigReward(campData.getRightBigReward());
            openBoxInfoMap = campData.getRightOpenBoxInfoMap();
        }
        List<Integer> keyList = new ArrayList<>(openBoxInfoMap.keySet());
        for (Integer boxId : keyList) {
            UnionCampWarOpenBoxInfo openBoxInfo = openBoxInfoMap.get(boxId);
            UnionCampWar.UnionCampBoxTemp.Builder boxMsg = UnionCampWar.UnionCampBoxTemp.newBuilder();
            boxMsg.setBoxId(boxId);
            boxMsg.setUserId(openBoxInfo.getUserId());
            boxMsg.setReward(openBoxInfo.getRewards());
            CrossUnionMember unionMember = CrossUnionMgr.getUnionMemberByUserId(openBoxInfo.getUserId());
            if (null != unionMember) {
                boxMsg.setUserName(unionMember.getUserBaseInfo().getNickName());
            }
            boxScenceMsg.addBoxInfo(boxMsg);
        }
    }

    public UnionCampWar.UnionCampBoxNoticeTemp addBoxNoticeData(UnionCampWarBoxNoticeData boxNoticeData) {
        synchronized (needInsertBoxNoticeList) {
            needInsertBoxNoticeList.add(boxNoticeData);
        }

        UnionCampWarCampInfo campInfo = getCampInfo(boxNoticeData.getCampId());
        UnionCampWar.UnionCampBoxNoticeTemp noticeTemp = UnionCampWarPb.parseBoxNoticeTempMsg(boxNoticeData).build();
        campInfo.addBoxNoticeMsg(noticeTemp);
        return noticeTemp;
    }

    public List<UnionCampWarBoxNoticeData> popNeedInsertBoxNoticeDataList() {
        synchronized (needInsertBoxNoticeList) {
            List<UnionCampWarBoxNoticeData> list = new ArrayList<>(needInsertBoxNoticeList);
            needInsertBoxNoticeList.clear();
            return list;
        }
    }

    public Map<Long, UnionCampWar.CrossUnionCampWarAutoSendRewardMsg.Builder> autoSendReward() {
        Map<Long, UnionCampWar.CrossUnionCampWarAutoSendRewardMsg.Builder> rewardMsgMap = new HashMap<>();

        int winCampId = this.campData.getWinCampId();
        for (UnionCampWarUserData warUserData : this.userDataMap.values()) {
            if (warUserData.getGetReward() != 0) {
                continue;
            }
            UnionCampWar.CrossUnionCampWarAutoSendRewardMsg.Builder builder = rewardMsgMap.get(warUserData.getServerId());
            if (null == builder) {
                builder = UnionCampWar.CrossUnionCampWarAutoSendRewardMsg.newBuilder();
                rewardMsgMap.put(warUserData.getServerId(), builder);
            }

//            String rewards = ConfigMgr.getUnionCampWarConfig().getRankRewards(warUserData.getCenterGroupId(), warUserData.getRank(), winCampId == warUserData.getCampId());
            String rewards = ConfigMgr.getUnionCampWarConfig().getCampRewards(warUserData.getCenterGroupId(), winCampId == warUserData.getCampId());
            warUserData.setGetReward(1);

            UnionCampWar.UnionCampWarSendRewardTemp.Builder rewardTemp = UnionCampWar.UnionCampWarSendRewardTemp.newBuilder();
            rewardTemp.setUserId(warUserData.getUserId());
            rewardTemp.setRewards(rewards);
            rewardTemp.setRank(warUserData.getRank());
            builder.addUserInfos(rewardTemp);
        }

        this.campData.setAutoSend(1);

        return rewardMsgMap;
    }

    public WorldMessageProto.WorldMessageTempMsg addChatMsg(WorldMessageProto.WorldMessageTempMsg chatMsg, long userId) {
        if (chatMsg.getType() == eMessageType.UnionCampWarPublicChat.getValue()) {
            synchronized (chatListMsg) {
                chatListMsg.addDataList(chatMsg);

                if (chatListMsg.getDataListCount() >= ConfigMgr.getUnionCampWarConfig().getChatNumMax()) {
                    chatListMsg.removeDataList(0);
                }
            }
        } else if (chatMsg.getType() == eMessageType.UnionCampWarCampChat.getValue()) {
            CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
            if (null == groupUnionData) {
                return null;
            }
            UnionCampWarCampInfo campInfo = getCampInfo(groupUnionData.getCampId());
            if (null == campInfo) {
                return null;
            }
            WorldMessageProto.WorldMessageTempMsg newChatMsg = chatMsg.toBuilder().setGroupId(campInfo.getCampId()).build();
            campInfo.addChatMsg(newChatMsg);
            return newChatMsg;
        }

        return chatMsg;
    }

    public WorldMessageProto.WorldMessageListMsg getChatListMsg(int messageType, long userId) {
        if (messageType == eMessageType.UnionCampWarPublicChat.getValue()) {
            synchronized (chatListMsg) {
                return chatListMsg.build();
            }
        } else if (messageType == eMessageType.UnionCampWarCampChat.getValue()) {
            CrossUnionCampWarGroupUnionData groupUnionData = CrossUnionCampWarLockGroupMgr.getGroupUnionData(userId);
            if (null == groupUnionData) {
                return null;
            }
            UnionCampWarCampInfo campInfo = getCampInfo(groupUnionData.getCampId());
            if (null == campInfo) {
                return null;
            }

            return campInfo.getChatListMsg();
        }

        return null;
    }

    public void addWorship(int campId) {
        synchronized (campData) {
            campData.addWorship(campId);
        }
    }

    public List<Integer> getWorshipList() {
        synchronized (campData) {
            List<Integer> worshipCount = new ArrayList<>();
            worshipCount.addAll(campData.getWorshipList());
            return worshipCount;
        }
    }

    public int getWorshipCount(int campId) {
        return campData.getWorship(campId);
    }


    /**
     * 新需求,打据点要按路线打
     *
     * @param holdId
     * @return
     */
    public boolean canAttackHold(int campId, int holdId) {
        UnionCampWarCampInfo campInfo = getCampInfo(campId);
        if (campInfo == null) {
            return false;
        }
        UnionCampWarHoldData holdInfo = campInfo.getHoldInfo(holdId);
        if (holdInfo == null) {
            return false;
        }
        if (holdInfo.getLeftHp() == 0) {
            return false;
        }

        UnionCampWarHoldData holdInfo1 = campInfo.getHoldInfo(1);
        if (holdInfo1.getLeftHp() == 0) {//第一的据点被击败就没路线限制了
            return true;
        }

        for (List<Integer> list : campData.getRouteListMap().values()) {
            for (Integer checkHoldId : list) {
                UnionCampWarHoldData checkHoldData = campInfo.getHoldInfo(checkHoldId);
                if (checkHoldData == null) {
                    continue;
                }
                if (checkHoldData.getLeftHp() == 0) {
                    continue;
                }
                if (checkHoldData.getHoldId() == holdId) {
                    return true;
                }
            }
        }

        return false;

    }

    public Map<Integer, List<UnionCampWarHoldData>> getHoldDataListMap() {
        Map<Integer, List<UnionCampWarHoldData>> holdDataListMap = new HashMap<>();
        for (UnionCampWarHoldData holdData : holdDataMap.values()) {
            List<UnionCampWarHoldData> holdDataList = holdDataListMap.get(holdData.getCampId());
            if (holdDataList == null) {
                holdDataList = new ArrayList<>();
                holdDataListMap.put(holdData.getCampId(), holdDataList);
            }
            holdDataList.add(holdData);
        }
        return holdDataListMap;
    }

    public boolean isEnemyHoldAlive(int campId) {
        int enemyCampId = campId == 1 ? 2 : 1;
        UnionCampWarCampInfo campInfo = getCampInfo(enemyCampId);
        return campInfo.isHoldAlive();
    }
}
